package com.example.lzy.photo;

import android.view.View;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.PopupWindow;

import com.example.lzy.viewpagerandfragment.R;


/**
 * Created by lzy on 2018/4/12.
 */

public class SelectPicPopup implements View.OnClickListener {
    /**
     * UI
     */
    private PopupWindow mPopupWindow;
    private Button mCameraBtn;//相册
    private Button mPicBtn;//图库
    private Button mCancelBtn;//取消
    private Context mContext;//上下文环境对象

    private OnBtnClickListener click;

    public PopupWindow getmPopupWindow() {
        return mPopupWindow;
    }

    /**
     * 单参构造
     * @param mContext
     */
    public SelectPicPopup(Context mContext) {
        this.mContext = mContext;
        mPopupWindow=new PopupWindow(mContext);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);//宽度充满父窗体
        mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);//高度包裹内容
        mPopupWindow.setTouchable(true);//设置可以被触摸
        mPopupWindow.setFocusable(true);//设置占据焦点
        mPopupWindow.setOutsideTouchable(true);//设置可以点击外部区域(点击外部区域将消失)
        mPopupWindow.setContentView(initView());
        mPopupWindow.setAnimationStyle(R.style.AnimBottom);//设置转场动画
        mPopupWindow.getContentView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mPopupWindow.setFocusable(false);
                dismiss();
                return false;
            }
        });
    }

    private View initView() {
        View view= LayoutInflater.from(mContext).inflate(R.layout.layout_popup,null);
        mCameraBtn= (Button) view.findViewById(R.id.camera_btn);
        mCameraBtn.setOnClickListener(this);

        mPicBtn= (Button) view.findViewById(R.id.pic_btn);
        mPicBtn.setOnClickListener(this);
        mCancelBtn= (Button) view.findViewById(R.id.cancel_btn);
        mCancelBtn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.camera_btn:
                click.getFlag(0);
                break;
            case R.id.pic_btn:
                click.getFlag(1);
                break;
            case R.id.cancel_btn:
                dismiss();
                break;
        }
    }

    public void dismiss(){
        if (mPopupWindow!=null&&mPopupWindow.isShowing()){
            mPopupWindow.dismiss();
        }
    }

    /**
     *
     * @param rootView
     */
    public void showPopup(View rootView){
        mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);
    }

    public interface OnBtnClickListener{
        void getFlag(int flag);
    }

    public void setOnBtnClick(OnBtnClickListener click){
        this.click=click;
    }
}

